package com.example.school.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.AdminGetListDto;
import com.example.school.dto.AdminSaveNotifyDto;
import com.example.school.dto.AdminUpdateNotifyDto;
import com.example.school.pojo.Notify;
import com.example.school.pojo.Orders;
import com.example.school.pojo.User;
import com.example.school.service.NotifyService;
import com.example.school.mapper.NotifyMapper;
import com.example.school.service.WebSocket;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.PageResponseResult;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author 24354
* @description 针对表【notify】的数据库操作Service实现
* @createDate 2024-03-16 19:11:47
*/
@Service
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, Notify>
    implements NotifyService{

    @Autowired
    private NotifyMapper notifyMapper;
    @Autowired
    private WebSocket webSocket;

    @Override
    public Result getNotifyList(AdminGetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Notify> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMimo()!=null) {
            wrapper.like(Notify::getNotifyName,dto.getMimo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result saveNotify(AdminSaveNotifyDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Notify notify=new Notify();
        notify= notify.builder()
                .notifyName(dto.getNotifyName())
                .notifyMessage(dto.getNotifyMessage())
                .notifyTime(String.valueOf(LocalDateTime.now())).build();
        int checkSave = notifyMapper.saveNotify(notify);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        Map<String, Object> map=new HashMap<>();
        map.put("time",notify.getNotifyTime());
        map.put("id",notify.getNotifyId());
        webSocket.sendToAllClient(JSON.toJSONString(map));
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeNotify(Long id) {
        if (id==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Notify checkNotify = notifyMapper.selectById(id);
        if (checkNotify==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        int checkRemove = notifyMapper.deleteById(id);
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateNotify(AdminUpdateNotifyDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Notify checkNotify = notifyMapper.selectById(dto.getNotifyId());
        Notify notify=new Notify();
        BeanUtils.copyProperties(checkNotify,notify);
        if (dto.getNotifyName()!=null&&!dto.getNotifyName().equals("")) {
            notify.setNotifyName(dto.getNotifyName());
        }
        if (dto.getNotifyMessage()!=null&&!dto.getNotifyMessage().equals("")) {
            notify.setNotifyMessage(dto.getNotifyMessage());
        }
        notify.setNotifyTime(String.valueOf(LocalDateTime.now()));
        int checkUpdate = notifyMapper.updateById(notify);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        Map<String, Object> map=new HashMap<>();
        map.put("time",notify.getNotifyTime());
        map.put("id",notify.getNotifyId());
        webSocket.sendToAllClient(JSON.toJSONString(map));
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result getUserNotifyList() {

        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        List<Notify> list = notifyMapper.selectList(Wrappers.<Notify>lambdaQuery().orderByDesc(Notify::getNotifyTime));

        return Result.okResult(list);
    }
}




